<?php
// +----------------------------------------------------------------------
// | Fanwe 方维p2p借贷系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------

class gamesModule extends baseModule
{
    public function __construct()
    {
        parent::__construct();
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/GamesRedisService.php');
        require_once APP_ROOT_PATH . 'mapi/lib/core/Model.class.php';
        Model::$lib = dirname(__FILE__);
    }

    protected static function getUserId()
    {
        $user_id = intval($GLOBALS['user_info']['id']);
        if (!$user_id) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '未登录',
            ));
        }
        return $user_id;
    }

    /**
     *
     */
    public function test()
    {
        $colors = array(
            'spade'   => 0,
            'heart'   => 1,
            'club'    => 2,
            'diamond' => 3,
        );
        $figures = array(
            'A'  => 1,
            '2'  => 2,
            '3'  => 3,
            '4'  => 4,
            '5'  => 5,
            '6'  => 6,
            '7'  => 7,
            '8'  => 8,
            '9'  => 9,
            '10' => 10,
            'J'  => 11,
            'Q'  => 12,
            'K'  => 13,
        );
        $number = $_REQUEST['number'];
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/tools/Poker.class.php');
        $poker = new Poker();
        $res   = $poker->play($number);
        $data  = array();
        foreach ($res as $key => $value) {
            $cards = array();
            foreach ($value['cards'] as $v) {
                $cards[] = array($colors[$v[0]], $figures[$v[1]]);
            }
            $data[] = array(
                'win'   => !$key,
                'cards' => $cards,
                'type'  => $value['check']['type'],
            );
        }
        api_ajax_return(array(
            'status' => 1,
            'data'   => $data,
        ));
    }

    public function init()
    {
        $user_id = self::getUserId();
        $table   = DB_PREFIX . 'games';
        $count   = $GLOBALS['db']->getOne("SELECT count(1) as count FROM $table");
        $list    = array();
        if ($count) {
            $field   = '`id`,`name`,`image`,`principal`';
            $list    = $GLOBALS['db']->getALL("SELECT $field FROM $table");
            $table   = DB_PREFIX . 'video';
            $video   = $GLOBALS['db']->getRow("SELECT `id` FROM  $table WHERE user_id=" . $user_id . " and live_in=1");
            $game_id = 0;
            if ($video['id']) {
                fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoRedisService.php');
                $video_redis = new VideoRedisService();
                $redis       = new GamesRedisService();
                $last_game   = $video_redis->getOne_db($video['id'], 'game_log_id');
                if ($last_game) {
                    $last_game = $redis->get($last_game, 'game_id,create_time,long_time');
                    if (NOW_TIME < $last_game['create_time'] + $last_game['long_time']) {
                        $game_id = $last_game['game_id'];
                    }
                }
            }
            foreach ($list as $key => $value) {
                $list[$key]['is_active'] = intval($value['id'] == $game_id);
                $list[$key]['image']     = get_abs_img_root($value['image']);
            }
        }
        api_ajax_return(array(
            'status'   => 1,
            'rs_count' => $count,
            'list'     => $list,
        ));
    }

    public function start()
    {
        $user_id = self::getUserId();
        $id      = intval($_REQUEST['id']);
        $table   = DB_PREFIX . 'games';
        $game    = $GLOBALS['db']->getRow("SELECT * FROM $table WHERE `id`=$id");
        if (!$game) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '游戏参数错误',
            ));
        }
        $table = DB_PREFIX . 'video';
        $video = $GLOBALS['db']->getRow("SELECT `id`,`group_id` FROM  $table WHERE `user_id`=$user_id AND `live_in`=1");

        $video_id = intval($video['id']);
        if (!($video_id && $video['group_id'])) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '不在直播状态',
            ));
        }
        $table   = DB_PREFIX . 'video_lianmai';
        $lianmai = $GLOBALS['db']->getRow("SELECT `id` FROM  $table WHERE `user_id`=$user_id AND `video_id`=$video_id AND `stop_time`=0");
        if ($lianmai) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '正在连麦，不能开始游戏',
            ));
        }
        $redis = new GamesRedisService();

        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoRedisService.php');
        $video_redis = new VideoRedisService();

        $last_log_id = $video_redis->getOne_db($video_id, 'game_log_id');
        if ($last_log_id) {
            $last_game = $redis->get($last_log_id, 'create_time,long_time');
            if (NOW_TIME < $last_game['create_time'] + $last_game['long_time']) {
                api_ajax_return(array(
                    'status'      => 0,
                    'game_log_id' => $last_log_id,
                    'error'       => '上局游戏未结束',
                ));
            }
        }
        if ($redis->isVideoLock($video_id)) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '正在开始游戏',
            ));
        }
        $redis->lockVideo($video_id);

        $data = array(
            'podcast_id'      => $user_id,
            'long_time'       => $game['long_time'],
            'game_id'         => $id,
            'create_time'     => NOW_TIME,
            'create_date'     => to_date(NOW_TIME, 'Y-m-d H:i:s'),
            'create_time_ymd' => to_date(NOW_TIME, 'Y-m-d'),
            'create_time_y'   => to_date(NOW_TIME, 'Y'),
            'create_time_m'   => to_date(NOW_TIME, 'm'),
            'create_time_d'   => to_date(NOW_TIME, 'd'),
        );
        $table = DB_PREFIX . 'game_log';
        $GLOBALS['db']->autoExecute($table, $data); // 插入数据库
        $game_log_id = $GLOBALS['db']->insert_id();
        $video_redis->update_db($video_id, array('game_log_id' => $game_log_id));

        $data['video_id']   = $video_id;
        $data['group_id']   = $video['group_id'];
        $data['option']     = $game['option'];
        $data['bet_option'] = $game['bet_option'];
        foreach (json_decode($game['option'], 1) as $key => $value) {
            $data[$key] = 0;
        }
        $data['status'] = 1;
        $redis->set($game_log_id, $data); // 插入redis
        $public_cards = array();
        if (in_array($id, array(3))) {
            $colors = array(
                'spade'   => 0,
                'heart'   => 1,
                'club'    => 2,
                'diamond' => 3,
            );
            $figures = array(
                'A'  => 1,
                '2'  => 2,
                '3'  => 3,
                '4'  => 4,
                '5'  => 5,
                '6'  => 6,
                '7'  => 7,
                '8'  => 8,
                '9'  => 9,
                '10' => 10,
                'J'  => 11,
                'Q'  => 12,
                'K'  => 13,
            );
            fanwe_require(APP_ROOT_PATH . 'mapi/lib/tools/Poker.class.php');
            $poker = new Poker();

            $public_cards = $poker->pickCards(1);
            $redis->set($game_log_id, array('gp' => json_encode($public_cards)));
            foreach ($public_cards as $key => $value) {
                $public_cards[$key] = array($colors[$value[0]], $figures[$value[1]]);
            }
        }
        // 新推送
        $ext = array(
            'type'        => 39,
            'desc'        => '',
            'room_id'     => $video_id,
            'time'        => $game['long_time'],
            'game_id'     => $id,
            'game_log_id' => $game_log_id,
            'game_status' => 1,
            'game_action' => 1,
            'option'      => array_values(json_decode($game['option'], 1)),
            'bet_option'  => json_decode($game['bet_option'], 1),
            'game_data'   => array(
                'public_cards' => $public_cards,
            ),
        );
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/tools/PushLog.class.php');
        PushLog::log(compact('ext', 'video'));
        $res = timSystemNotify($video['group_id'], $ext);
        PushLog::log(compact('res'));
        // if (IS_DEBUG) {
        //     fanwe_require(APP_ROOT_PATH . 'mapi/lib/tools/PushLog.class.php');
        //     PushLog::log(compact('res', 'ext', 'video'));
        // }
        $redis->unLockVideo($video_id);
        if ($game_log_id) {
            api_ajax_return(array(
                'status'      => 1,
                'game_log_id' => $game_log_id,
            ));
        } else {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '服务器繁忙',
            ));
        }
    }

    public function bet()
    {
        $user_id = self::getUserId();
        $id      = intval($_REQUEST['id']);
        $bet     = intval($_REQUEST['bet']);
        $money   = intval($_REQUEST['money']);
        $key     = array(1 => 'option1', 2 => 'option2', 3 => 'option3');
        if (!isset($key[$bet])) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '参数错误',
            ));
        }

        $field = array(
            'status',
            'podcast_id',
            'create_time',
            'long_time',
        );

        $redis = new GamesRedisService();
        $game  = $redis->get($id, $field);
        if (!$game['status']) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '游戏不存在',
            ));
        }
        if (intval($game['create_time'] + $game['long_time'] - NOW_TIME) <= 0) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '超出投注时间',
            ));
        }
        $user_model = Model::build('user');
        $res        = $user_model->coin($user_id, -$money);

        if (!$res) {
            api_ajax_return(array(
                'status'           => 0,
                'error'            => '金额不足',
                'account_diamonds' => intval($account_diamonds),
            ));
        }
        $account_diamonds = $user_model->coin($user_id);
        Model::build('coin_log')->addLog($user_id, $id, -$money, $account_diamonds, '游戏投注');

        $data = array(
            'game_log_id' => $id,
            'user_id'     => $user_id,
            'money'       => $money,
            'bet'         => $bet,
            'podcast_id'  => $game['podcast_id'],
            'create_time' => NOW_TIME,
            'create_date' => to_date(NOW_TIME, 'Y-m-d H:i:s'),
        );
        $table = DB_PREFIX . 'user_game_log';
        $GLOBALS['db']->autoExecute($table, $data);

        $redis = new GamesRedisService();
        $res   = $redis->inc($id, $key[$bet], $money);
        $res   = $res && $redis->inc($id, $key[$bet] . ':' . $user_id, $money);
        $field = array(
            'status',
            'group_id',
            'podcast_id',
            'create_time',
            'long_time',
            'game_id',
            'video_id',
        );
        for ($i = 1; $i <= 3; $i++) {
            $field[] = 'option' . $i;
            $field[] = 'option' . $i . ':' . $user_id;
        }
        $data     = $redis->get($id, $field);
        $bet      = array();
        $user_bet = array();
        for ($i = 1; $i <= 3; $i++) {
            $bet[]      = intval($data['option' . $i]);
            $user_bet[] = intval($data['option' . $i . ':' . $user_id]);
        }
        $time = intval($data['create_time'] + $data['long_time'] - NOW_TIME);
        $time = $time > 0 ? $time : 0;

        // 新推送
        $ext = array(
            'type'        => 39,
            'room_id'     => intval($data['video_id']),
            'desc'        => '',
            'time'        => $time,
            'game_id'     => intval($data['game_id']),
            'game_log_id' => $id,
            'game_status' => intval($data['status']),
            'game_action' => 2,
            'game_data'   => array(
                'bet' => $bet,
            ),
        );
        $res = timSystemNotify($data['group_id'], $ext);
        if (IS_DEBUG) {
            fanwe_require(APP_ROOT_PATH . 'mapi/lib/tools/PushLog.class.php');
            PushLog::log(compact('res', 'ext', 'data'));
        }

        api_ajax_return(array(
            'status' => intval($res),
            'data'   => array(
                'game_status'      => $data['status'],
                'time'             => $time,
                'account_diamonds' => $account_diamonds,
                'game_data'        => array(
                    'bet'      => $bet,
                    'user_bet' => $user_bet,
                ),
            ),
        ));
    }

    public function get_video()
    {
        $id       = intval($_REQUEST['id']);
        $video_id = intval($_REQUEST['video_id']);
        if (!$id && $video_id) {
            fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoRedisService.php');
            $video_redis = new VideoRedisService();
            $id          = $video_redis->getOne_db($video_id, 'game_log_id');
            $id          = intval($id);
        }
        if (!$id && !$video_id) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '参数错误',
            ));
        }
        $user_id = self::getUserId();
        $redis   = new GamesRedisService();
        $field   = array(
            'video_id',
            'group_id',
            'status',
            'game_id',
            'create_time',
            'long_time',
            'option',
            'bet_option',
            'public_cards',
        );
        for ($i = 1; $i <= 3; $i++) {
            $field[] = 'option' . $i;
            $field[] = 'option' . $i . ':' . $user_id;
            $field[] = 'option_win' . $i;
            $field[] = 'option_cards' . $i;
            $field[] = 'option_type' . $i;
        }
        $data = $redis->get($id, $field);
        if ($data['game_id'] === false) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '游戏不存在',
            ));
        }
        $public_cards = json_decode($data['public_cards'], 1);
        $win          = 0;
        $bet          = array();
        $user_bet     = array();
        $cards_data   = array();
        for ($i = 1; $i <= 3; $i++) {
            if (intval($data['option_win' . $i])) {
                $win = $i;
            }
            $bet[]        = intval($data['option' . $i]);
            $user_bet[]   = intval($data['option' . $i . ':' . $user_id]);
            $cards        = json_decode($data['option_cards' . $i], 1);
            $type         = intval($data['option_type' . $i]);
            $cards_data[] = array(
                'cards' => $cards ? $cards : array(),
                'type'  => $type ? $type : 0,
            );
        }
        $time = intval($data['create_time'] + $data['long_time'] - NOW_TIME);

        $ext = array(
            'type'        => 39,
            'user_id'     => $user_id,
            'desc'        => '',
            'room_id'     => $data['video_id'],
            'time'        => $time > 0 ? $time : 0,
            'game_id'     => $data['game_id'],
            'game_log_id' => $id,
            'game_status' => $data['status'],
            'game_action' => 6,
            'option'      => array_values(json_decode($data['option'], 1)),
            'bet_option'  => json_decode($data['bet_option'], 1),
            'game_data'   => array(
                'public_cards' => $public_cards ? $public_cards : array(),
                'win'          => $win,
                'bet'          => $bet,
                'user_bet'     => $user_bet,
                'cards_data'   => $cards_data,
            ),
        );
        $res = timSystemNotify($data['group_id'], $ext, array(trim($user_id)));
        if (IS_DEBUG) {
            fanwe_require(APP_ROOT_PATH . 'mapi/lib/tools/PushLog.class.php');
            PushLog::log(compact('res', 'ext', 'video'));
        }
        api_ajax_return(array(
            'status'  => 1,
            'error'   => '',
            'data'    => $ext,
            'game_id' => $data['game_id'],
        ));
    }

    public function log()
    {
        $podcast_id = intval($_REQUEST['podcast_id']);
        $game_id    = intval($_REQUEST['game_id']);
        if (!$podcast_id || !$game_id) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '参数错误',
            ));
        }
        $number = intval($_REQUEST['number']);
        $number = $number ? $number : 20;
        $table  = DB_PREFIX . 'game_log';
        $data   = $GLOBALS['db']->getAll(
            "SELECT
                `result`
            FROM
                $table
            WHERE
                podcast_id = $podcast_id
            AND game_id = $game_id
            AND STATUS = 2
            AND `result` > 0
            ORDER BY
                `id` DESC
            LIMIT $number"
        );
        // $win    = array(0, 0, 0);
        $res = array();
        foreach ($data as $value) {
            if ($value['result']) {
                // foreach ($win as $k => $v) {
                //     $win[$k] = $value['result'] == $k + 1 ? 1 : 0;
                // }
                // $res[] = $win;
                $res[] = intval($value['result']);
            }
        }
        api_ajax_return(array(
            'status' => 1,
            'data'   => $res,
        ));

    }

    public function stop()
    {

        $user_id = self::getUserId();
        $table   = DB_PREFIX . 'video';
        $video   = $GLOBALS['db']->getRow("SELECT `id`,`group_id` FROM  $table WHERE user_id=" . $user_id . " and live_in=1");

        if (!$video['id']) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '不在直播状态',
            ));
        }

        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoRedisService.php');
        $video_redis = new VideoRedisService();
        $redis       = new GamesRedisService();

        if ($redis->isVideoLock($video['id'])) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '正在开始游戏',
            ));
        }
        $redis->lockVideo($video['id']);
        $last_log_id = $video_redis->getOne_db($video['id'], 'game_log_id');
        if ($last_log_id) {
            $last_game = $redis->get($last_log_id, 'create_time,long_time,game_id,status');
            if (NOW_TIME < $last_game['create_time'] + $last_game['long_time']) {
                $redis->set($last_log_id, array('long_time' => 0));
                $sql = "update " . DB_PREFIX . "game_log set long_time = 0 where id = " . $last_log_id;
                $GLOBALS['db']->query($sql);
            }
        } else {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '不在游戏状态',
            ));
            die;
        }
        $video_redis->update_db($video['id'], array('game_log_id' => 0));
        $ext = array(
            'type' => 34,
            'desc' => '',
        );
        $res = timSystemNotify($video['group_id'], $ext);
        if (IS_DEBUG) {
            fanwe_require(APP_ROOT_PATH . 'mapi/lib/tools/PushLog.class.php');
            PushLog::log(compact('res', 'ext', 'video'));
        }
        $redis->unLockVideo($video['id']);
        api_ajax_return(array(
            'status' => 1,
        ));
    }

    public function userDiamonds()
    {
        $game_log_id = intval($_REQUEST['id']);
        $user_id     = self::getUserId();
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserRedisService.php');
        $redis = new UserRedisService();
        // $user_diamonds = $redis->getOne_db($user_id, 'diamonds');
        $user_diamonds = $redis->getOne_db($user_id, 'coin');
        $gain          = 0;
        if ($game_log_id) {
            $table = DB_PREFIX . 'user_game_log';
            $gain  = $GLOBALS['db']->getOne("SELECT `money` FROM  $table WHERE `game_log_id`=$game_log_id AND `user_id`=$user_id AND `bet`=0 AND `type`=2");
        }
        api_ajax_return(array(
            'status'        => 1,
            'user_diamonds' => intval($user_diamonds),
            'gain'          => intval($gain),
        ));
    }
    public function exchangeRate()
    {
        $m_config = load_auto_cache("m_config");
        $rate     = $m_config['coin_exchange_rate'] ? floatval($m_config['coin_exchange_rate']) : 1;
        api_ajax_return(array(
            'status'        => 1,
            'exchange_rate' => $rate,
        ));
    }
    public function exchangeCoin()
    {
        $user_id  = self::getUserId();
        $diamonds = intval($_REQUEST['diamonds']);
        if ($diamonds < 1) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '请输入兑换钻石',
            ));
        }
        Connect::beginTransaction();
        //减少用户钻石
        $sql = "update " . DB_PREFIX . "user set diamonds = diamonds - " . $diamonds . "  where id = '" . $user_id . "' and diamonds >= " . $diamonds;
        $GLOBALS['db']->query($sql);
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserRedisService.php');
        $user_redis = new UserRedisService();
        if ($GLOBALS['db']->affected_rows()) {
            $user_redis->inc_field($user_id, 'diamonds', -$diamonds);
            // user_deal_to_reids(array(intval($user_id)));

            $time = NOW_TIME;

            $account_diamonds = $user_redis->getOne_db(intval($user_id), 'diamonds');
            //会员账户 钻石变更日志表
            $diamonds_log_data = array(
                'pai_id'           => 0,
                'user_id'          => $user_id,
                'diamonds'         => $diamonds, //变更数额
                'account_diamonds' => $account_diamonds, //账户余额
                'memo'             => '钻石兑换游戏币', //备注
                'create_time'      => $time,
                'create_date'      => to_date($time, 'Y-m-d H:i:s'),
                'create_time_ymd'  => to_date($time, 'Y-m-d'),
                'create_time_y'    => to_date($time, 'Y'),
                'create_time_m'    => to_date($time, 'm'),
                'create_time_d'    => to_date($time, 'd'),
                'type'             => 5, //youxi
            );
            $GLOBALS['db']->autoExecute(DB_PREFIX . "user_diamonds_log", $diamonds_log_data);
            $user_model = Model::build('user');
            $m_config   = load_auto_cache("m_config");
            $rate       = $m_config['coin_exchange_rate'] ? floatval($m_config['coin_exchange_rate']) : 1;
            $res        = $user_model->coin($user_id, $diamonds * $rate);
            if (!$res) {
                Connect::rollback();
                api_ajax_return(array(
                    'status' => 0,
                    'error'  => '兑换失败',
                ));
            }
            $coin = $user_model->coin($user_id);
            Model::build('coin_log')->addLog($user_id, -1, $diamonds * $rate, $coin, '钻石兑换游戏币');
            Connect::commit();
            api_ajax_return(array(
                'status'           => 1,
                'account_diamonds' => $account_diamonds,
                'coin'             => $coin,
            ));
        }
        Connect::rollback();
        api_ajax_return(array(
            'status' => 0,
            'error'  => '余额不足',
        ));
    }
    public function sendCoin()
    {
        $user_id    = self::getUserId();
        $coin       = intval($_REQUEST['coin']);
        $to_user_id = intval($_REQUEST['to_user_id']);
        if (!($user_id && $to_user_id && $coin > 0)) {
            api_ajax_return(array(
                'status' => 0,
                'error'  => '参数错误',
            ));
        }
        $user_model = Model::build('user');
        Connect::beginTransaction();
        $res = $user_model->coin($user_id, -$coin);
        if (!$res) {
            Connect::rollback();
            api_ajax_return(array(
                'status' => 0,
                'error'  => '余额不足',
            ));
        }
        $res = $user_model->coin($to_user_id, $coin);
        if (!$res) {
            Connect::rollback();
            api_ajax_return(array(
                'status' => 0,
                'error'  => '数据库出错',
            ));
        }
        $coin_log_model = Model::build('coin_log');

        $coin1     = $user_model->coin($user_id);
        $nick_name = $user_model->getOneById($user_id, 'nick_name');
        $nick_name = $nick_name['nick_name'];
        $coin_log_model->addLog($user_id, -1, -$coin, $coin1, "赠送($nick_name)游戏币");
        $coin2     = $user_model->coin($to_user_id);
        $nick_name = $user_model->getOneById($to_user_id, 'nick_name');
        $nick_name = $nick_name['nick_name'];
        $coin_log_model->addLog($to_user_id, -1, $coin, $coin, "收到($nick_name)游戏币");
        Connect::commit();
        api_ajax_return(array(
            'status'       => 1,
            'error'        => '赠送成功',
            'from_msg'     => "送给你{$coin}游戏币",
            'to_msg'       => "收到{$coin}游戏币",
            'from_score'   => "",
            'to_ticket'    => 0,
            'to_diamonds'  => 0,
            'to_user_id'   => $to_user_id,
            'prop_icon'    => get_domain() . '/public/gift/jinbi.png',
            'prop_id'      => 0,
            'total_ticket' => 0,
        ));
    }

    public function __destruct()
    {
        parent::__destruct();
    }
    public function pushTest()
    {
        $user_id        = self::getUserId();
        $test_system_im = json_decode($_REQUEST['test_system_im']);
        fanwe_require(APP_ROOT_PATH . 'mapi/lib/tools/PushLog.class.php');
        PushLog::log(compact('user_id', 'test_system_im'));
        api_ajax_return(array('status' => 1));
    }
    public function pushTest2()
    {
        $id    = intval($_REQUEST['id']);
        $table = DB_PREFIX . 'video';
        $video = $GLOBALS['db']->getRow("SELECT `id`,`group_id` FROM  $table WHERE `live_in`=1 AND `user_id`=$id");
        if ($video) {
            $ext = array(
                'type'        => 39,
                'desc'        => '',
                'room_id'     => $video['id'],
                'time'        => 60,
                'game_id'     => 2,
                'game_log_id' => 1,
                'game_status' => 1,
                'game_action' => 1,
                'option'      => array(3, 3, 3),
                'bet_option'  => array(10, 100, 1000, 10000),
                'game_data'   => array(
                    'public_cards' => array(),
                ),
            );
            $res = timSystemNotify($video['group_id'], $ext);
        }
        echo "<pre>";
        var_dump(compact('video', 'res', 'ext'));
        if ($video) {
            usleep(100000);
            $ext = json_decode('{"type":39,"desc":"","room_id":"' . $video['id'] . '","time":0,"game_id":"2","game_log_id":"1","game_status":2,"game_action":4,"podcast_income":0,"game_data":{"win":3,"bet":[0,0,0],"cards_data":[{"cards":[[3,11],[0,10],[0,8],[0,1],[1,9]],"type":6},{"cards":[[3,8],[0,9],[2,7],[3,10],[3,2]],"type":8},{"cards":[[2,8],[1,4],[3,3],[3,12],[0,3]],"type":6}]}}', 1);
            $res = timSystemNotify($video['group_id'], $ext);
            var_dump(compact('res', 'ext'));
        }
    }
}
